﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Fis.Win.Utils;

namespace Fis.Win
{
    public partial class MainForm : DevExpress.XtraEditors.XtraForm
    {

        Agile.Engine.View.IViewProvider syncProvider = Fis.Win.Utils.AppSession.GetViewProvider("SynchronizeViewProvider");

        public void SetMainControl(UserControl control)
        {
            //control.Size = panelMain.ClientSize;
            //control.DataBindings.Clear();
            //control.DataBindings.Add("Size", panelMain, "ClientSize");
            control.Dock = DockStyle.Fill;
            panelMain.Controls.Clear();
            panelMain.Controls.Add(control);
        }

        public MainForm()
        {
            InitializeComponent();
        }

        bool needCancel = true;

        private void MainForm_Load(object sender, EventArgs e)
        {
            var view = UIManager.ShowEdit<Fis.Win.Forms.LoginForm>("LoginViewProvider");
            if (((DialogResult)view.ViewResult) != DialogResult.OK)
            {
                needCancel = false;
                Close();
                return;
            }

            needCancel = true;
            Fis.Win.Utils.UIUtils.InitializeNavBarControl(navBarControl);
            InitializeAidsMenu();
            (syncProvider as Fis.Win.Rules.SynchronizeViewProvider).Start();
        }

        void InitializeAidsMenu()
        {
            var group = Fis.Win.Utils.UIUtils.ViewConfig.Tables["aidsgroup"];
            foreach (var d in group.AsEnumerable())
            {
                var model = new System.Collections.Hashtable();
                model["key"] = d["key"];
                iAids.AddItem(Fis.Win.Utils.UIUtils.CreateBarItem<DevExpress.XtraBars.BarButtonItem>
                                (barManager1, null, d["caption"].ToString(), 0,
                                (sender, e) =>
                                {
                                    if (Fis.Win.Utils.AppSession.RightsValidata("BD.AIDS", "View"))
                                        UIManager.ShowList<Forms.AidsListForm>("AidsListViewProvider", model);
                                }
                                ));
                iAidsExport.AddItem(Fis.Win.Utils.UIUtils.CreateBarItem<DevExpress.XtraBars.BarButtonItem>
                (barManager1, null, d["caption"].ToString(), 0,
                (sender, e) =>
                {
                    if (Fis.Win.Utils.AppSession.RightsValidata("BD.AIDS", "View"))
                        UIManager.ShowList<Forms.AidsListForm>("AidsListViewProvider", model);
                }
                ));

            }
        }

        private void iRate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BD.Rate", "View"))
                UIManager.ShowList<Forms.RateListForm>("RateListViewProvider");
        }

        private void iItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BD.AIDS", "View"))

                UIManager.ShowList<Forms.AidsListForm>("AidsListViewProvider");
        }

        private void iAccount_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BD.ACCOUNT", "View"))
                UIManager.ShowList<Forms.AccountListForm>("AccountListViewProvider");

        }

        private void iBillEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "AddNew"))
                UIManager.ShowEdit<Forms.BillEditForm>("BillEditViewProvider");
        }

        private void iBillQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "Query"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                using (var query = new Fis.Win.Forms.BillQueryForm())
                {
                    if (query.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        return;
                    }

                    model["filter0"] = query.FilterMaster;
                    model["filter1"] = query.FilterDetail;
                }


                UIManager.ShowList<Forms.BillListForm>("BillListViewProvider", model);
            }
        }

        private void iInitialize_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("INIT", "AddNew"))
            {
                if (Fis.Win.Utils.AppSession.IsComplateInitialize)
                {
                    XtraMessageBox.Show("已结束初始化");
                    return;
                }

                System.Collections.Hashtable m = new System.Collections.Hashtable();
                m["isInitialize"] = true;
                UIManager.ShowEdit<Forms.BillEditForm>("BillEditViewProvider", Agile.Engine.View.ViewState.AddNew, m);
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (needCancel)
            {
                if (XtraMessageBox.Show("确定要退出吗？", "", MessageBoxButtons.YesNo) != System.Windows.Forms.DialogResult.Yes)
                {
                    e.Cancel = true;
                    return;
                }

                (syncProvider as Fis.Win.Rules.SynchronizeViewProvider).Stop();
            }

            Application.ExitThread();
        }

        private void iBookInfo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "Modify"))
            {
                UIManager.ShowEdit<Fis.Win.Forms.BookInfoForm>("BookInfoProvider", Agile.Engine.View.ViewState.Modify);
                AppSession.InitializeBookInfo();
            }
        }

        private void iCloseInitialize_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.IsComplateInitialize)
            {
                XtraMessageBox.Show("已结束初始化");
                return;
            }
            if (Fis.Win.Utils.AppSession.RightsValidata("INIT", "CLOSE"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                model["FormName"] = "结束初始化";
                UIManager.ShowList<Forms.AccountTreatForm>("AccountTreatViewProvider", model);
            }
        }

        public void InitializeStatusInfo(System.Collections.Hashtable model)
        {
            siName.Caption = string.Format("账套名称：{0}", model["FNAME"]);
            siYear.Caption = string.Format("当前年度：{0}", DateTime.Parse(model["FBOOKSTARTIME"].ToString()).Year);
            siRange.Caption = string.Format("当前期间：{0}", model["FBOOKRANGE"]);
            siUser.Caption = string.Format("当前用户：{0}", Fis.Win.Utils.AppSession.UserName);
        }

        public string NetStatus
        {
            set { siState.Caption = value; }
        }

        private void iBillSubmit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "Submit"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                model["filter0"] = " AND FSYNC<1";
                model["filter1"] = string.Empty;

                UIManager.ShowList<Forms.BillListForm>("BillListViewProvider", model);
            }
        }

        private void iBalance_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("REPORT.BALANCE", "View"))
            {
                using (var f = new Fis.Win.Forms.BalanceReportForm())
                {
                    f.ShowDialog();
                }
            }
        }

        private void iIncome_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("REPORT.INCOME", "View"))
            {
                using (var f = new Fis.Win.Forms.IncomeReportForm())
                {
                    f.ShowDialog();
                }
            }
        }

        private void iUserInfo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("UserListForm", "View"))
                UIManager.ShowList<Fis.Win.Forms.UserListForm>("UserListViewProvider");
        }

        private void iLogin_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            (syncProvider as Fis.Win.Rules.SynchronizeViewProvider).Stop();
            var view = UIManager.ShowEdit<Fis.Win.Forms.LoginForm>("LoginViewProvider");
            (syncProvider as Fis.Win.Rules.SynchronizeViewProvider).Start();
        }

        private void iExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            needCancel = true;
            Close();
        }

        private void iChangePwd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var param = new System.Collections.Hashtable();
            UIManager.ShowEdit<Fis.Win.Forms.UserPwdForm>("UserEditViewProvider", Agile.Engine.View.ViewState.Modify, param);
        }

        private void iCertificateAudit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "AUDIT"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                model["FormName"] = "凭证审核";
                UIManager.ShowList<Forms.AccountTreatForm>("AccountTreatViewProvider", model);
            }
        }

        private void iCertificatePosting_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "POST"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                model["FormName"] = "凭证过账";
                UIManager.ShowList<Forms.AccountTreatForm>("AccountTreatViewProvider", model);
            }
        }

        private void iTransferProfit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!Fis.Win.Utils.AppSession.IsComplateInitialize)
            {
                XtraMessageBox.Show("请先结束初始化");
                return;
            }
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "TRANS"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                model["FormName"] = "结转损益";
                UIManager.ShowList<Forms.AccountTreatForm>("AccountTreatViewProvider", model);
            }
        }

        private void iAccountSettle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!Fis.Win.Utils.AppSession.IsComplateInitialize)
            {
                XtraMessageBox.Show("请先结束初始化");
                return;
            }
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "CLOSE"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                model["FormName"] = "结账";
                UIManager.ShowList<Forms.AccountTreatForm>("AccountTreatViewProvider", model);
            }
        }

        private void iBookReport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("REPORT.BOOK", "VIEW"))
            {
                var f = new Fis.Win.Forms.BookReportForm();
                f.ShowDialog();
            }
        }

        private void iInitQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("INIT", "QUERY"))
            {
                if (Fis.Win.Utils.AppSession.IsComplateInitialize)
                {
                    XtraMessageBox.Show("已结束初始化");
                    return;
                }

                System.Collections.Hashtable model = new System.Collections.Hashtable();
                model["filter0"] = " AND FSTATUS=4";
                model["filter1"] = string.Empty;
                model["listtype"] = "init";

                UIManager.ShowList<Forms.BillListForm>("BillListViewProvider", model);
            }
        }

        private void iBookRate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!Fis.Win.Utils.AppSession.IsComplateInitialize)
            {
                XtraMessageBox.Show("请先结束初始化");
                return;
            }
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "RATE"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                model["FormName"] = "期末调汇";
                UIManager.ShowList<Forms.AccountTreatForm>("AccountTreatViewProvider", model);
            }
        }

        private void iDetailReport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("REPORT.DETAIL", "VIEW"))
            {
                var f = new Fis.Win.Forms.DetailReportForm();
                f.ShowDialog();
            }

        }

        private void iAccAmount_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("REPORT.REST", "VIEW"))
            {
                var f = new Fis.Win.Forms.RestReportForm();
                f.ShowDialog();
            }

        }

        private void iNewYearBook_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "NEWYEARBOOK"))
            {
                if (int.Parse(Fis.Win.Utils.AppSession.Range) <= 12)
                {
                    XtraMessageBox.Show("请先进行年度结账");
                    return;
                }
                UIManager.ShowList<Forms.NewYearBookForm>("NewYearBookProvider");
            }
        }

        private void iRetunBook_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!Fis.Win.Utils.AppSession.RightsValidata("INIT", "PREVBOOK"))
                return;

            var result = XtraMessageBox.Show("退回上年度账套将删除本账套信息，是否继续？", "", MessageBoxButtons.YesNo);
            if (result == System.Windows.Forms.DialogResult.No)
                return;

            var provider = Fis.Win.Utils.AppSession.GetViewProvider("BookInfoProvider");
            if (provider.ExecuteUpdateXtra(new System.Collections.Hashtable()))
            {
                Fis.Win.Utils.AppSession.InitializeBookInfo();
                XtraMessageBox.Show(String.Format("账套已退回到{0}年度", Fis.Win.Utils.AppSession.Year));
                return;
            }

            XtraMessageBox.Show(provider.Message);
        }

        private void iSync_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            bool exec = false;

            Fis.Win.Forms.LoadingForm.ShowLoading(
                () => { exec = syncProvider.ExecuteUpdateXtra(null); }, null
            );

            XtraMessageBox.Show((exec) ? "同步成功。" : syncProvider.Message);
        }

        private void iInitRate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("INIT", "IMPORT"))
            {
                var model = new System.Collections.Hashtable();
                model["TransType"] = 3;
                UIManager.ShowList<Forms.ImportWizardForm>("ImportViewProvider", model);
            }
        }

        private void iTaxRate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "NEWYEARBOOK"))
            {
                UIManager.ShowEdit<Forms.TaxRateEditForm>("TaxRateViewProvider");
            }
        }

        private void iEnterprise_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "NEWYEARBOOK"))
            {
                UIManager.ShowEdit<Forms.EnterpriseViewForm>("EnterpriseViewProvider", Agile.Engine.View.ViewState.Modify);
            }
        }

        private void iAidsInfo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "AIDSINFO"))
            {
                UIManager.ShowEdit<Forms.BookAidsViewForm>("BookAidsViewProvider");
            }

        }

        private void iInitAids_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("INIT", "IMPORT"))
            {
                var model = new System.Collections.Hashtable();
                model["TransType"] = 2;
                UIManager.ShowList<Forms.ImportWizardForm>("ImportViewProvider", model);
            }
        }

        private void iInitAcc_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("INIT", "IMPORT"))
            {
                var model = new System.Collections.Hashtable();
                model["TransType"] = 1;
                UIManager.ShowList<Forms.ImportWizardForm>("ImportViewProvider", model);
            }
        }

        private void iInitBill_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("INIT", "IMPORT"))
            {
                var model = new System.Collections.Hashtable();
                model["TransType"] = 0;
                UIManager.ShowList<Forms.ImportWizardForm>("ImportViewProvider", model);
            }
        }

        private void iReverseInitialize_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!Fis.Win.Utils.AppSession.IsComplateInitialize)
            {
                XtraMessageBox.Show("当前已经是未结束初始化状态");
                return;
            }
            if (Fis.Win.Utils.AppSession.RightsValidata("INIT", "CLOSE"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                model["FormName"] = "反初始化";
                UIManager.ShowList<Forms.AccountTreatForm>("AccountTreatViewProvider", model);
            }
        }

        private void iReverseCertificate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "POST"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                model["FormName"] = "凭证反过账";
                UIManager.ShowList<Forms.AccountTreatForm>("AccountTreatViewProvider", model);
            }
        }

        private void iReverseAccountSettle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!Fis.Win.Utils.AppSession.IsComplateInitialize)
            {
                XtraMessageBox.Show("请先结束初始化");
                return;
            }
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "CLOSE"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                model["FormName"] = "反结账";
                UIManager.ShowList<Forms.AccountTreatForm>("AccountTreatViewProvider", model);
            }
        }

        private void iBillReport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "Query"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                using (var query = new Fis.Win.Forms.BillQueryForm())
                {
                    if (query.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        return;
                    }

                    model["filter0"] = query.FilterMaster;
                    model["filter1"] = query.FilterDetail;
                }

                UIManager.ShowList<Forms.BillListForm>("BillListViewProvider", model);
            }
        }

        private void iRateExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BD.Rate", "View"))
                UIManager.ShowList<Forms.RateListForm>("RateListViewProvider");

        }

        private void iExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BD.ACCOUNT", "View"))
                UIManager.ShowList<Forms.AccountListForm>("AccountListViewProvider");

        }

        private void iBillExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("BOOK", "Query"))
            {
                System.Collections.Hashtable model = new System.Collections.Hashtable();
                using (var query = new Fis.Win.Forms.BillQueryForm())
                {
                    if (query.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    {
                        return;
                    }

                    model["filter0"] = query.FilterMaster;
                    model["filter1"] = query.FilterDetail;
                }

                UIManager.ShowList<Forms.BillListForm>("BillListViewProvider", model);
            }
        }

        private void iPreRest_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Fis.Win.Utils.AppSession.RightsValidata("REPORT.REST", "VIEW"))
            {
                var f = new Fis.Win.Forms.PreRestReportForm();
                f.ShowDialog();
            }
        }
    }
}